<template>
  <div>
    <vacationTable
      :tableData="checkList"
      :loading="false"
      style="margin-bottom: 20px"
      v-if="checkList.length"
    >
    </vacationTable>
    <el-button
      style="width: 100%"
      type="primary"
      icon="el-icon-plus"
      plain
      @click="handleAdd"
      v-if="!lookView"
      >{{ $t("leave") }}</el-button
    >
    <el-dialog
      :title="$t('select')"
      :visible.sync="open"
      width="800px"
      append-to-body
    >
      <el-form :model="queryParams"  @submit.native.prevent>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('holiday_name')" label-width="80px">
              <el-input
                v-model="queryParams.vacationName"
                :placeholder="$t('please_enter', { name: $t('holiday_name') })"
                @keyup.enter.native="getList"
              ></el-input
            ></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item>
              <el-button
                style="margin-left: 20px"
                size="mini"
                type="primary"
                icon="el-icon-search"
                @click="getList"
                >{{ $t("search") }}</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <vacationTable
        ref="vacationTable"
        :tableData="scheduleList"
        :checkData="checkList"
        :loading="loading"
        :isSelect="true"
        @handleSelectionChange="handleSelectionChange"
        v-if="open"
      ></vacationTable>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
        :page-sizes="[10,20,30,50,100, 200]"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{
          $t("confirm")
        }}</el-button>
        <el-button @click="closeModal">{{ $t("cancel") }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import vacationTable from "@/views/custom/vacationList/vacationTable.vue";
import { listSchedule } from "@/api/custom/vacationList";
export default {
  name: "vacationModal",
  components: {
    vacationTable,
  },
  props: {
    checkData: {
      type: [Array, null],
      default() {
        return [];
      },
    },
    lookView: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 假期表格数据
      scheduleList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      open: false,
      checkList: [],
      temporaryCheckList: [],
    };
  },
  watch: {
    checkData: {
      handler(val) {
        if (val) {
          this.setCheckList(val);
        } else {
          this.setCheckList([]);
        }
      },
      deep: true,
      immediate: true,
    },
  },
  created() {},
  methods: {
    setCheckList(list) {
      this.checkList = list;
    },
    handleAdd() {
      this.open = true;
      this.queryParams.pageNum = 1;
      this.getList();
    },
    closeModal() {
      this.open = false;
    },
    /** 查询假期列表 */
    getList() {
      this.loading = true;
      listSchedule(this.queryParams).then((response) => {
        this.scheduleList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.temporaryCheckList = selection;
    },
    submitForm() {
      this.checkList = [...this.temporaryCheckList];
      this.open = false;
    },
  },
};
</script>
